package com.wangzhixuan.commons.utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;

public class AjaxUtils {

	public static String JSON(Object data) {
		return com.alibaba.fastjson.JSON.toJSONString(data);
	}
	public static String JSONDate(Object data) {
		return JSON.toJSONStringWithDateFormat(data,DateUtil.DatePattern.YYYY_MM_DD_HH_MM_SS);
	}
	public static <T> String JSONPage(List<T> data) {
		PageInfo<T> pageInfo = new PageInfo<T>(data);
		return JSON.toJSONStringWithDateFormat(pageInfo,DateUtil.DatePattern.YYYY_MM_DD_HH_MM_SS);
	}

	private static Map<String, Object> jsonObj(String statusCode, String jsonName, Object json) {
		Map<String, Object> jsonObj = new HashMap<String, Object>();
		jsonObj.put(Global.JSON_STATUS_NAME, statusCode);
		jsonObj.put(jsonName, json);
		return jsonObj;
	}

	private static Map<String, Object> jsonObj(String jsonName, Object json) {
		Map<String, Object> jsonObj = new HashMap<String, Object>();
		jsonObj.put(jsonName, json);
		return jsonObj;
	}

	/**
	 * 返回Json成功消息
	 * 
	 * @return Map<String,Object>
	 */
	public static Map<String, Object> returnSuccMsg() {
		return jsonObj(Global.AJAX_SUCCESS_CODE, Global.JSON_MSG_NAME, Global.AJAX_SUCCESS_MSG);
	}

	/**
	 * 自定义返回Json成功消息
	 * 
	 * @param msg
	 * @return
	 */
	public static Map<String, Object> returnSuccMsg(String msg) {
		return jsonObj(Global.AJAX_SUCCESS_CODE, Global.JSON_MSG_NAME, msg);
	}

	/**
	 * 返回Json失败消息
	 * 
	 * @return Map<String,Object>
	 */
	public static Map<String, Object> returnFailMsg() {
		return jsonObj(Global.AJAX_ERROR_CODE, Global.JSON_MSG_NAME, Global.AJAX_ERROR_MSG);
	}

	/**
	 * 自定义返回Json失败消息
	 * 
	 * @param msg
	 * @return
	 */
	public static Map<String, Object> returnFailMsg(String msg) {
		return jsonObj(Global.AJAX_ERROR_CODE, Global.JSON_MSG_NAME, msg);
	}

	public static Map<String, Object> returnData(String jsonName, Object obj) {
		return jsonObj(jsonName, obj);
	}
}
